package cn.caojun.auth.controller;

import cn.caojun.auth.dto.LoginDTO;
import cn.caojun.auth.service.ILoginService;
import cn.caojun.basic.utils.AjaxResult;
import cn.caojun.basic.utils.BasicMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginDTO loginDTO){
        try{
            Map<String,Object> map= loginService.login(loginDTO);
            return AjaxResult.me().setObject(map);
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("登陆失败"+e.getMessage());
        }
    }

    @PostMapping("/logout")
    public AjaxResult logout(HttpServletRequest request){
        try{
            String token=request.getHeader("token");
            BasicMap.loginMap.remove(token);
            return AjaxResult.me();
        }catch (Exception e){
            return AjaxResult.me().setSuccess(false).setMessage("退出登录失败"+e.getMessage());
        }
    }
}
